<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入物料名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产工单" prop="productionOrder">
        <el-input
          v-model="queryParams.productionOrder"
          placeholder="请输入生产工单"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['prodmanage:return:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['prodmanage:return:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['prodmanage:return:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['prodmanage:return:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="returnList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="领料类型" align="center" prop="pickingType" :formatter="pickingTypeFormat" sortable width="150"/>
      <el-table-column label="领料部门" align="center" prop="pickingDepartment" sortable width="150"/>
      <el-table-column label="所属车间" align="center" prop="workshop" sortable width="150"/>
      <el-table-column label="产线" align="center" prop="prodLine" sortable width="150"/>
      <el-table-column label="物料代码" align="center" prop="code" sortable width="150"/>
      <el-table-column label="物料名称" align="center" prop="name" sortable width="150"/>
      <el-table-column label="规格型号" align="center" prop="goodsModel" sortable width="150"/>
      <el-table-column label="生产工单" align="center" prop="productionOrder" sortable width="150"/>
      <el-table-column label="退料数量" align="center" prop="returnQuanatity" sortable width="150"/>
      <el-table-column label="申请数量" align="center" prop="applyNum" sortable width="150"/>
      <el-table-column label="实发数量" align="center" prop="actualNum" sortable width="150"/>
      <el-table-column label="单位" align="center" prop="unit" sortable width="150"/>
      <el-table-column label="发料仓库" align="center" prop="lssuingWarehouse" sortable width="150" />
      <el-table-column label="仓位" align="center" prop="position" sortable width="150"/>
      <el-table-column label="工序号" align="center" prop="procedureNum" sortable width="150"/>
      <el-table-column label="工序" align="center" prop="workProcedure" sortable width="150"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="150" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createUser" width="150" sortable/>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="150" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateUser" sortable width="150"/>
      <el-table-column label="审核状态" align="center" prop="auditStatus" :formatter="auditStatusFormat" sortable width="150"/>
      <!-- <el-table-column label="打印次数" align="center" prop="printsNum" />
      <el-table-column label="对方科目" align="center" prop="oppositeAccount" />
      <el-table-column label="倒冲标记" align="center" prop="backflushMark" />
      <el-table-column label="领料用途" align="center" prop="pickingUse" />
      <el-table-column label="原单类型" align="center" prop="originalType" />
      <el-table-column label="退料id" align="center" prop="returnId" />
      <el-table-column label="物料id" align="center" prop="goodsId" />
            <el-table-column label="选单号" align="center" prop="menuNum" />
            <el-table-column label="原单单号" align="center" prop="originalOrder" />
            <el-table-column label="助记码" align="center" prop="auxiliaryCode" />
      <el-table-column label="是否返工" align="center" prop="isRework" />
      <el-table-column label="成本对象" align="center" prop="costObject" />
      <el-table-column label="成本对象代码" align="center" prop="costObjectCode" />
      <el-table-column label="产品规格型号" align="center" prop="productModel" />
      <el-table-column label="成本对象组" align="center" prop="costGroup" />
      <el-table-column label="work号" align="center" prop="workNum" />
      <el-table-column label="项目编号" align="center" prop="projectCode" />
      <el-table-column label="可用库存" align="center" prop="availableStock" />
      <el-table-column label="辅助单位" align="center" prop="auxiliaryUnit" />
      <el-table-column label="换算率" align="center" prop="conversionRate" />
      <el-table-column label="辅助数量" align="center" prop="auxiliaryNum" />
      <el-table-column label="计划单价" align="center" prop="planUnivalence" />
      <el-table-column label="计划价金额" align="center" prop="planPrice" />
      <el-table-column label="单价" align="center" prop="univalence" />
      <el-table-column label="金额" align="center" prop="price" />
      <el-table-column label="客户bom" align="center" prop="customerBom" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="生产/采购日期" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保质期" align="center" prop="guaranteePeriod" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.guaranteePeriod, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期至" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
       <el-table-column label="成本中心" align="center" prop="costCenter" />
      <el-table-column label="位置号" align="center" prop="locationNum" />
      <el-table-column label="还料尺寸" align="center" prop="returnDimension" />
      <el-table-column label="还料书" align="center" prop="returnBook" />
      <el-table-column label="已复核数量" align="center" prop="checkedQuantity" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['prodmanage:return:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['prodmanage:return:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产管理生产退料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入物料id" />
        </el-form-item>
        <el-form-item label="物料代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入物料代码" />
        </el-form-item>
        <el-form-item label="助记码" prop="auxiliaryCode">
          <el-input v-model="form.auxiliaryCode" placeholder="请输入助记码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="goodsModel">
          <el-input v-model="form.goodsModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="原单单号" prop="originalOrder">
          <el-input v-model="form.originalOrder" placeholder="请输入原单单号" />
        </el-form-item>
        <el-form-item label="生产任务单号" prop="productionOrder">
          <el-input v-model="form.productionOrder" placeholder="请输入生产任务单号" />
        </el-form-item>
        <el-form-item label="退料数量" prop="returnQuanatity">
          <el-input v-model="form.returnQuanatity" placeholder="请输入退料数量" />
        </el-form-item>
        <el-form-item label="申请数量" prop="applyNum">
          <el-input v-model="form.applyNum" placeholder="请输入申请数量" />
        </el-form-item>
        <el-form-item label="实发数量" prop="actualNum">
          <el-input v-model="form.actualNum" placeholder="请输入实发数量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="发料仓库" prop="lssuingWarehouse">
          <el-input v-model="form.lssuingWarehouse" placeholder="请输入发料仓库" />
        </el-form-item>
        <el-form-item label="是否返工" prop="isRework">
          <el-input v-model="form.isRework" placeholder="请输入是否返工" />
        </el-form-item>
        <el-form-item label="成本对象" prop="costObject">
          <el-input v-model="form.costObject" placeholder="请输入成本对象" />
        </el-form-item>
        <el-form-item label="成本对象代码" prop="costObjectCode">
          <el-input v-model="form.costObjectCode" placeholder="请输入成本对象代码" />
        </el-form-item>
        <el-form-item label="产品规格型号" prop="productModel">
          <el-input v-model="form.productModel" placeholder="请输入产品规格型号" />
        </el-form-item>
        <el-form-item label="成本对象组" prop="costGroup">
          <el-input v-model="form.costGroup" placeholder="请输入成本对象组" />
        </el-form-item>
        <el-form-item label="work号" prop="workNum">
          <el-input v-model="form.workNum" placeholder="请输入work号" />
        </el-form-item>
        <el-form-item label="项目编号" prop="projectCode">
          <el-input v-model="form.projectCode" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="可用库存" prop="availableStock">
          <el-input v-model="form.availableStock" placeholder="请输入可用库存" />
        </el-form-item>
        <el-form-item label="辅助单位" prop="auxiliaryUnit">
          <el-input v-model="form.auxiliaryUnit" placeholder="请输入辅助单位" />
        </el-form-item>
        <el-form-item label="换算率" prop="conversionRate">
          <el-input v-model="form.conversionRate" placeholder="请输入换算率" />
        </el-form-item>
        <el-form-item label="辅助数量" prop="auxiliaryNum">
          <el-input v-model="form.auxiliaryNum" placeholder="请输入辅助数量" />
        </el-form-item>
        <el-form-item label="计划单价" prop="planUnivalence">
          <el-input v-model="form.planUnivalence" placeholder="请输入计划单价" />
        </el-form-item>
        <el-form-item label="计划价金额" prop="planPrice">
          <el-input v-model="form.planPrice" placeholder="请输入计划价金额" />
        </el-form-item>
        <el-form-item label="单价" prop="univalence">
          <el-input v-model="form.univalence" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="金额" prop="price">
          <el-input v-model="form.price" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="客户bom" prop="customerBom">
          <el-input v-model="form.customerBom" placeholder="请输入客户bom" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="生产/采购日期" prop="date">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生产/采购日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保质期" prop="guaranteePeriod">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.guaranteePeriod"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择保质期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期至" prop="expiryDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.expiryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择有效期至">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="仓位" prop="position">
          <el-input v-model="form.position" placeholder="请输入仓位" />
        </el-form-item>
        <el-form-item label="工序号" prop="procedureNum">
          <el-input v-model="form.procedureNum" placeholder="请输入工序号" />
        </el-form-item>
        <el-form-item label="工序" prop="workProcedure">
          <el-input v-model="form.workProcedure" placeholder="请输入工序" />
        </el-form-item>
        <el-form-item label="成本中心" prop="costCenter">
          <el-input v-model="form.costCenter" placeholder="请输入成本中心" />
        </el-form-item>
        <el-form-item label="位置号" prop="locationNum">
          <el-input v-model="form.locationNum" placeholder="请输入位置号" />
        </el-form-item>
        <el-form-item label="还料尺寸" prop="returnDimension">
          <el-input v-model="form.returnDimension" placeholder="请输入还料尺寸" />
        </el-form-item>
        <el-form-item label="还料书" prop="returnBook">
          <el-input v-model="form.returnBook" placeholder="请输入还料书" />
        </el-form-item>
        <el-form-item label="已复核数量" prop="checkedQuantity">
          <el-input v-model="form.checkedQuantity" placeholder="请输入已复核数量" />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-select v-model="form.auditStatus" placeholder="请选择审核状态">
            <el-option
              v-for="dict in auditStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打印次数" prop="printsNum">
          <el-input v-model="form.printsNum" placeholder="请输入打印次数" />
        </el-form-item>
        <el-form-item label="对方科目" prop="oppositeAccount">
          <el-input v-model="form.oppositeAccount" placeholder="请输入对方科目" />
        </el-form-item>
        <el-form-item label="倒冲标记" prop="backflushMark">
          <el-input v-model="form.backflushMark" placeholder="请输入倒冲标记" />
        </el-form-item>
        <el-form-item label="领料类型" prop="pickingType">
          <el-select v-model="form.pickingType" placeholder="请选择领料类型">
            <el-option
              v-for="dict in pickingTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领料部门" prop="pickingDepartment">
          <el-input v-model="form.pickingDepartment" placeholder="请输入领料部门" />
        </el-form-item>
        <el-form-item label="领料用途" prop="pickingUse">
          <el-input v-model="form.pickingUse" placeholder="请输入领料用途" />
        </el-form-item>
        <el-form-item label="原单类型" prop="originalType">
          <el-select v-model="form.originalType" placeholder="请选择原单类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属车间" prop="workshop">
          <el-input v-model="form.workshop" placeholder="请输入所属车间" />
        </el-form-item>
        <el-form-item label="选单号" prop="menuNum">
          <el-input v-model="form.menuNum" placeholder="请输入选单号" />
        </el-form-item>
        <el-form-item label="产线id" prop="prodLine">
          <el-input v-model="form.prodLine" placeholder="请输入产线id" />
        </el-form-item>
                        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReturn, getReturn, delReturn, addReturn, updateReturn, exportReturn } from "@/api/prodmanage/return";

export default {
  name: "Return",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产管理生产退料表格数据
      returnList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核状态字典
      auditStatusOptions: [],
      // 领料类型字典
      pickingTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: undefined,
        code: undefined,
        auxiliaryCode: undefined,
        name: undefined,
        goodsModel: undefined,
        originalOrder: undefined,
        productionOrder: undefined,
        returnQuanatity: undefined,
        applyNum: undefined,
        actualNum: undefined,
        unit: undefined,
        lssuingWarehouse: undefined,
        isRework: undefined,
        costObject: undefined,
        costObjectCode: undefined,
        productModel: undefined,
        costGroup: undefined,
        workNum: undefined,
        projectCode: undefined,
        availableStock: undefined,
        auxiliaryUnit: undefined,
        conversionRate: undefined,
        auxiliaryNum: undefined,
        planUnivalence: undefined,
        planPrice: undefined,
        univalence: undefined,
        price: undefined,
        customerBom: undefined,
        remark: undefined,
        date: undefined,
        guaranteePeriod: undefined,
        expiryDate: undefined,
        position: undefined,
        procedureNum: undefined,
        workProcedure: undefined,
        costCenter: undefined,
        locationNum: undefined,
        returnDimension: undefined,
        returnBook: undefined,
        checkedQuantity: undefined,
        createTime: undefined,
        createUser: undefined,
        updateTime: undefined,
        updateUser: undefined,
        isDel: undefined,
        auditStatus: undefined,
        printsNum: undefined,
        oppositeAccount: undefined,
        backflushMark: undefined,
        pickingType: undefined,
        pickingDepartment: undefined,
        pickingUse: undefined,
        originalType: undefined,
        workshop: undefined,
        menuNum: undefined,
        prodLine: undefined,
        source: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("prodmanage_audit_status").then(response => {
      this.auditStatusOptions = response.data;
    });
    this.getDicts("prodmanage_up_picking_type").then(response => {
      this.pickingTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询生产管理生产退料列表 */
    getList() {
      this.loading = true;
      listReturn(this.queryParams).then(response => {
        this.returnList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 审核状态字典翻译
    auditStatusFormat(row, column) {
      return this.selectDictLabel(this.auditStatusOptions, row.auditStatus);
    },
    // 领料类型字典翻译
    pickingTypeFormat(row, column) {
      return this.selectDictLabel(this.pickingTypeOptions, row.pickingType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        returnId: undefined,
        goodsId: undefined,
        code: undefined,
        auxiliaryCode: undefined,
        name: undefined,
        goodsModel: undefined,
        originalOrder: undefined,
        productionOrder: undefined,
        returnQuanatity: undefined,
        applyNum: undefined,
        actualNum: undefined,
        unit: undefined,
        lssuingWarehouse: undefined,
        isRework: undefined,
        costObject: undefined,
        costObjectCode: undefined,
        productModel: undefined,
        costGroup: undefined,
        workNum: undefined,
        projectCode: undefined,
        availableStock: undefined,
        auxiliaryUnit: undefined,
        conversionRate: undefined,
        auxiliaryNum: undefined,
        planUnivalence: undefined,
        planPrice: undefined,
        univalence: undefined,
        price: undefined,
        customerBom: undefined,
        remark: undefined,
        date: undefined,
        guaranteePeriod: undefined,
        expiryDate: undefined,
        position: undefined,
        procedureNum: undefined,
        workProcedure: undefined,
        costCenter: undefined,
        locationNum: undefined,
        returnDimension: undefined,
        returnBook: undefined,
        checkedQuantity: undefined,
        createTime: undefined,
        createUser: undefined,
        updateTime: undefined,
        updateUser: undefined,
        isDel: undefined,
        auditStatus: undefined,
        printsNum: undefined,
        oppositeAccount: undefined,
        backflushMark: undefined,
        pickingType: undefined,
        pickingDepartment: undefined,
        pickingUse: undefined,
        originalType: undefined,
        workshop: undefined,
        menuNum: undefined,
        prodLine: undefined,
        source: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.returnId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产管理生产退料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const returnId = row.returnId || this.ids
      getReturn(returnId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产管理生产退料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.returnId != null) {
            updateReturn(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReturn(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const returnIds = row.returnId || this.ids;
      this.$confirm('是否确认删除生产管理生产退料编号为"' + returnIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReturn(returnIds);
        }).then(response => {
          this.getList();
          this.msgSuccess(response.msg);
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有生产管理生产退料数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReturn(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
